package com.kevin.util.html.tag;

import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.kevin.util.Function;

import lombok.Data;

public @Data class CheckboxTag  extends TagSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 134546676L;
	private String name = "";
	private String value;
	private String show;
	
	

	

	@SuppressWarnings("unchecked")
	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		
		try {	
			StringBuffer newSb = new StringBuffer("");
			newSb.append("<input type=\"checkbox\" name=\""+name+"\" ");
			newSb.append(" value=\""+value+"\" ");
			ServletRequest req =  pageContext.getRequest();

			if(req != null && req.getAttribute("data") != null){
				Object obj = req.getAttribute("data");
				Object val = Function.getValue(obj, name);
				if(val instanceof List){
					List<Object> list = (List<Object>)val;
					for(Object o:list){
						int dataid = (Integer)Function.getValue(o, "id");
						if(value.equals(dataid+"")){
							newSb.append(" checked=\"true\" ");
							break;
						}
					}
				}else{
					if(value.equals(val+"")){
						newSb.append(" checked=\"true\" ");
					}
				}
			}
			newSb.append(" />");
			newSb.append(show);
			pageContext.getOut().print(newSb.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		return EVAL_PAGE;
	}
}
